package org.jeecg.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.banner.entity.WeixinBanner;
import org.jeecg.modules.banner.service.IWeixinBannerService;
import org.jeecg.modules.weixin.entity.WeixinMessage;
import org.jeecg.modules.weixin.entity.WeixinMessageDetail;
import org.jeecg.modules.weixin.service.IWeixinMessageDetailService;
import org.jeecg.modules.weixin.service.IWeixinMessageService;
import org.jeecg.modules.weixin.vo.WeixinMessagePage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/api/user")
public class UserApiController {
    @Autowired
    private IWeixinBannerService weixinBannerService;

    /**
     * 查询轮播图
     * @param weixinBanner
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/bannerlist")
    public Result<?> bannerlist(WeixinBanner weixinBanner,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WeixinBanner> queryWrapper = QueryGenerator.initQueryWrapper(weixinBanner, req.getParameterMap());
        Page<WeixinBanner> page = new Page<WeixinBanner>(pageNo, pageSize);
        IPage<WeixinBanner> pageList = weixinBannerService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
    @Autowired
    private IWeixinMessageService weixinMessageService;
    @Autowired
    private IWeixinMessageDetailService weixinMessageDetailService;

    @RequestMapping(value = "/messagelist")
    public Result<?> messagelist(WeixinMessage weixinMessage,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WeixinMessage> queryWrapper = QueryGenerator.initQueryWrapper(weixinMessage, req.getParameterMap());
        Page<WeixinMessage> page = new Page<WeixinMessage>(pageNo, pageSize);
        IPage<WeixinMessage> pageList = weixinMessageService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 点赞
     * @param weixinMessagePage
     * @return
     */
    @RequestMapping(value = "/editmessage")
    public Result<?> editmessage( WeixinMessagePage weixinMessagePage) {
        WeixinMessage weixinMessageEntity = weixinMessageService.getById(weixinMessagePage.getId());
        if(weixinMessageEntity==null) {
            return Result.error("未找到对应数据");
        }
        weixinMessageEntity.setGoodcount(weixinMessageEntity.getGoodcount()+1);
      weixinMessageService.updateById(weixinMessageEntity);
        return Result.ok("编辑成功!");
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/messagequeryById")
    public Map queryById(@RequestParam(name="id",required=true) String id) {
        Map map = new HashMap();
        WeixinMessage weixinMessage = weixinMessageService.getById(id);
        map.put("message",weixinMessage);
        List<WeixinMessageDetail> weixinMessageDetailList = weixinMessageDetailService.selectByMainId(id);
        map.put("messagedetail",weixinMessageDetailList);
        return map;

    }
    /**
     *   添加
     *
     * @param weixinMessagePage
     * @return
     */
    @RequestMapping(value = "/addmessage")
    public Result<?> addmessage( WeixinMessagePage weixinMessagePage,HttpServletRequest request) {
        WeixinMessage weixinMessage = new WeixinMessage();
        BeanUtils.copyProperties(weixinMessagePage, weixinMessage);
        String paths = request.getParameter("paths");
        List<WeixinMessageDetail> message = new ArrayList();
        if(!StringUtils.isEmpty(paths)){
            String temp[] = paths.split(",");
            WeixinMessageDetail weixinMessageDetail ;
            for(String s:temp){
                if(!StringUtils.isEmpty(s)){
                    weixinMessageDetail = new WeixinMessageDetail();
                    weixinMessageDetail.setPath(s);
                    message.add(weixinMessageDetail);
                }


            }

        }
        weixinMessage.setGoodcount(new Random().nextInt(10) + 1);
        weixinMessageService.saveMain(weixinMessage, message);
        return Result.ok("添加成功！");
    }

}
